import java.util.Random;
import java.util.Scanner;

public class ArrayDExample {
    public static void main(String[] args) {
        int rows = 3;  // 行数
        int cols = 4;  // 列数
        int[][] a = new int[rows][cols];

        randomInitialize(a);  // 随机初始化数组
        printArray(a);        // 打印数组

        Scanner scanner = new Scanner(System.in);
        // 通过键盘录入数据初始化数组
        inputInitialize(a, scanner);
        printArray(a);        // 打印数组

        System.out.println("按列求和: " + java.util.Arrays.toString(columnSum(a)));
        System.out.println("按行求和: " + java.util.Arrays.toString(rowSum(a)));
        System.out.println("最大的一行索引: " + maxRow(a));
        System.out.println("最大的一列索引: " + maxColumn(a));
        System.out.println("数组最大值: " + maxValue(a));
        System.out.println("数组最小值: " + minValue(a));

        scanner.close();
    }

    // 随机初始化二维数组
    public static void randomInitialize(int[][] array) {
        Random random = new Random();
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                array[i][j] = random.nextInt(100);  // 0-99的随机数
            }
        }
    }

    // 通过键盘录入数据初始化二维数组
    public static void inputInitialize(int[][] array, Scanner scanner) {
        System.out.println("请输入数组的元素:");
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.printf("输入元素[%d][%d]: ", i, j);
                array[i][j] = scanner.nextInt();
            }
        }
    }

    // 按列求和
    public static int[] columnSum(int[][] array) {
        int[] sum = new int[array[0].length];
        for (int j = 0; j < array[0].length; j++) {
            for (int i = 0; i < array.length; i++) {
                sum[j] += array[i][j];
            }
        }
        return sum;
    }

    // 按行求和
    public static int[] rowSum(int[][] array) {
        int[] sum = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                sum[i] += array[i][j];
            }
        }
        return sum;
    }

    // 求数组中最大的一行
    public static int maxRow(int[][] array) {
        int maxSum = Integer.MIN_VALUE;
        int maxRowIndex = -1;

        for (int i = 0; i < array.length; i++) {
            int rowSum = 0;
            for (int j = 0; j < array[i].length; j++) {
                rowSum += array[i][j];
            }
            if (rowSum > maxSum) {
                maxSum = rowSum;
                maxRowIndex = i;
            }
        }
        return maxRowIndex;
    }

    // 求数组中最大的一列
    public static int maxColumn(int[][] array) {
        int maxSum = Integer.MIN_VALUE;
        int maxColIndex = -1;

        for (int j = 0; j < array[0].length; j++) {
            int colSum = 0;
            for (int i = 0; i < array.length; i++) {
                colSum += array[i][j];
            }
            if (colSum > maxSum) {
                maxSum = colSum;
                maxColIndex = j;
            }
        }
        return maxColIndex;
    }

    // 求数组的最大值
    public static int maxValue(int[][] array) {
        int max = Integer.MIN_VALUE;
        for (int[] row : array) {
            for (int value : row) {
                if (value > max) {
                    max = value;
                }
            }
        }
        return max;
    }

    // 求数组的最小值
    public static int minValue(int[][] array) {
        int min = Integer.MAX_VALUE;
        for (int[] row : array) {
            for (int value : row) {
                if (value < min) {
                    min = value;
                }
            }
        }
        return min;
    }

    // 打印二维数组
    public static void printArray(int[][] array) {
        System.out.println("数组内容:");
        for (int[] row : array) {
            for (int value : row) {
                System.out.printf("%4d", value);
            }
            System.out.println();
        }
    }
}

